//
//  SystemAuthorizationClass.m
//  VideoExam
//
//  Created by apple on 2020/4/23.
// 张帅

#import "SystemAuthorizationClass.h"
#import <AVFoundation/AVCaptureDevice.h>
#import <AVFoundation/AVMediaFormat.h>
@import AssetsLibrary;
@implementation SystemAuthorizationClass

#pragma mark 选择相册访问权限
+(BOOL)ask_photo_permissions_bloack:(void(^__nullable)(BOOL allow))block {
//    PHAuthorizationStatusNotDetermined = 0, // 默认还没做出选择
//
//    PHAuthorizationStatusRestricted,        // 此应用程序没有被授权访问的照片数据
//
//    PHAuthorizationStatusDenied,            // 用户已经明确否认了这一照片数据的应用程序访问
//
//    PHAuthorizationStatusAuthorized         //  用户已经授权应用访问照片数据
    
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    if (status==PHAuthorizationStatusNotDetermined) {// 默认还没做出选择
           [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
             if(status == PHAuthorizationStatusAuthorized) {
                 dispatch_async(dispatch_get_main_queue(), ^{
                     // 用户点击 "OK"
                     if (block) {
                        block(YES);
                     }
                 });
             } else {
                 dispatch_async(dispatch_get_main_queue(), ^{
                     // 用户点击 不允许访问
                     if (block) {
                        block(NO);
                    }
                 });
             }
         }];
        return NO;
    } else if (status == PHAuthorizationStatusDenied ) {//拒绝
           UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"温馨提示"
                                                                                message:@"需要您跳转到设置界面去打开相册权限"
                                                                         preferredStyle:UIAlertControllerStyleAlert];
           [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
        [alert addAction:[UIAlertAction actionWithTitle:@"去打开" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                      [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];//跳转设置界面
                 }]];
          UIViewController *vc = [AppTools zs_getCurrentViewController];
           [vc presentViewController:alert animated:YES completion:nil];
        return NO;
    } else if (status == PHAuthorizationStatusAuthorized) {//接受
        return YES;
    }
    
    return NO;
}
#pragma mark 摄像头访问权限
+ (BOOL)ask_camera_permissions_bloack:(void(^__nullable)(BOOL allow))block  {
//    AVAuthorizationStatusNotDetermined = 0,// 系统还未知是否访问，第一次开启相机时
//
//    AVAuthorizationStatusRestricted, // 受限制的
//
//    AVAuthorizationStatusDenied, //不允许
//
//    AVAuthorizationStatusAuthorized // 允许状态
    AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (status==AVAuthorizationStatusNotDetermined) {// 默认还没做出选择
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
            if (granted) {//同意
                 dispatch_async(dispatch_get_main_queue(), ^{
                     if (block) {
                         block(YES);
                     }
                 });
           } else {//拒绝
               dispatch_async(dispatch_get_main_queue(), ^{
                   if (block) {
                       block(NO);
                   }
               });
           }
        }];
        return NO;
    } else if (status == AVAuthorizationStatusDenied ) {//拒绝
        [SystemAuthorizationClass to_set_type:2];//提示
        return NO;
    } else if (status == AVAuthorizationStatusAuthorized) {//接受
        return YES;
    }
    return NO;
}

+ (BOOL)audio_set_block:(void(^__nullable)(BOOL allow))block {
    //status 1：被拒绝  2： 没弹窗  3  有授权
   int status = [SystemAuthorizationClass get_current_Audio_Status];
     if (status == 2) {//没弹窗 没请求过
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
            if (granted) {//同意
                  dispatch_async(dispatch_get_main_queue(), ^{
                      if (block) {
                          block(YES);
                      }
                  });
                
            } else {//拒绝
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (block) {
                        block(NO);
                    }
                });
            }
        }];
         return NO;
     } else if (status == 1) {//1：被拒绝
         [SystemAuthorizationClass to_set_type:3];//type 1 ： 相册  2：摄像头  3：麦克风
           return NO;
     } else if (status == 3) {// 3  有授权
         return YES;
     }
    
    return NO;
 
}



/// 获取当前麦克风权限状态 返回状态 1：被拒绝  2： 没弹窗  3  有授权
+ (int)get_current_Audio_Status{
    AVAuthorizationStatus microPhoneStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
           switch (microPhoneStatus) {
              case AVAuthorizationStatusDenied:
              case AVAuthorizationStatusRestricted:
              {
                  return 1;
                

              }
                  break;
              case AVAuthorizationStatusNotDetermined:
              {
                    return 2;
//                  if (block) {
//                      block(2);
//                }
                  // 没弹窗
    //              [self requestMicroPhoneAuth];
//                   [QMUITips showInfo:@"没弹窗"];
              }
                  break;
              case AVAuthorizationStatusAuthorized:
              {
                  return 3;
                  // 有授权
//                  [QMUITips showInfo:@"有授权"];
              }
                  break;
    
              default:
                  break;
          }
        
}

/// 弹窗提示 跳转设置 打开相关权限
/// @param type 1 ： 相册  2：摄像头  3：麦克风
+ (void)to_set_type:(int)type {
    NSString * message_str;
    if (type==1) {
        message_str=@"需要您跳转到设置界面去打开相册权限";
    } else if (type ==2) {
       message_str=@"需要您跳转到设置界面去打开摄像头权限";
    } else if (type ==3) {
       message_str=@"需要您跳转到设置界面去打开麦克风权限";
    }
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:message_str preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [alert addAction:[UIAlertAction actionWithTitle:@"去打开" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
          [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];//跳转设置界面
    }]];
    UIViewController *vc = [AppTools zs_getCurrentViewController];
    [vc presentViewController:alert animated:YES completion:nil];
}
//AVAuthorizationStatus videoAuthStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];



@end
